/**
 * 小工具或小控件集
 * 
 * @author wonderful HongXin
 * @create 20180126
 */

(function() {
	littileTools = {
		/**
		 * fileInfosStr 文件信息的json格式字符串或单个文件信息对象
		 * focusObj 控件要定位到的元素jquery对象
		 * docDir 文件所在的目录地址
		 * isAsync 是否异步获取文件信息，如果是，则会触发从服务端指定接口获取。
		 * asyncOpt:{
		 *  url:"",获取文件信息的服务端地址
		 *  param:"",请求参数
		 *  callBack:function(filesListObj,resultData){//文件列表对象|服务端范围的结果对象
		 *  
		 *  } 
		 * }
		 */
        //下载文件列表
		filesListPanel : {
			show : function (fileInfosStr, focusObj,
					docDir,isAsync,asyncOpt) {

				
				$(".tempFileList").remove();
				var fileList = $("<div class='tempFileList'></div>").appendTo(
						"body");
				var contentTable = $("<table class='contentTable'></table>")
						.appendTo(fileList);
				var resultIframe = $(
						"<iframe name='downLoadProjectDocFileResult' style='width:0px;height:0px'></iframe>")
						.appendTo(fileList)
                fileList.bind('click',function(e){
                    stopPropagation(e);
                });



                if(isAsync){
					
					$.ajax({
           	         url:asyncOpt.url,
           	         //processData: false,
           	         //data: xmlDocument,
           	         async: true,
           	         dataType: "json",
           	         type: "POST",
           	         contentType: "application/x-www-form-urlencoded",
           	         // data: encodeURI(option.param),//解决页面默认为GBK编码导致的Ajax传输中文乱码问题
           	         data:asyncOpt.param,
           	         beforeSend: null,
           	         success: function (data) {
           	        	 if(data.status){
           	 
           	        		$(data.data)
							.each(
									function(i,fileInfo) {
										var fileInfoTypeArr=fileInfo.type.split("/");
										var fileInfoType = fileInfo.type;
										var tr = $("<tr></tr>").appendTo(
												contentTable);
										var td = $("<td></td>").appendTo(tr);
										var nameLink = $(
												"<a href='"
														+ docDir
														+ "/"
														+ fileInfo.filePath
														+ "' download='' target='downLoadProjectDocFileResult' >"
													//	+ fileInfo.fileName
														+ "</a>").appendTo(td);
										var dl = $('<dl></dl>').appendTo(nameLink);
										var dt = $('<dt class="'+fileInfoType+'"></dt>').appendTo(dl);
										var fileName = $('<span class="downLoadFileName">'+fileInfo.fileName+'</span>').appendTo(dt);
										var fileSize = $('<span class="downLoadFileSize">文件大小：'+new Number(fileInfo.size/1024).toFixed(2)+'KB</span>').appendTo(dt);
										var fileDownloadBtn = $("<dd><span></span></dd>").appendTo(dl);
										// if (fileInfo.type && fileInfo.type != "") {
                                        //
										// 	fileTypeTd
										// 			.text(fileInfo.type
										// 					.indexOf(".") == 0 ? fileInfo.type
										// 					.substr(1)
										// 					: fileInfo.type);
										// }
										// var fileSize = $(
										// 		"<td width='20%'>" + new Number(fileInfo.size/1024).toFixed(2)
										// 				+ "KB</td>").appendTo(tr);

									});
           	        		
           	        		if(asyncOpt.callBack){
           	        			
           	        			asyncOpt.callBack(fileList,data);
           	        			
           	        		}
           	        		
           	        	 }
           	        	 
           	       		var left = focusObj.offset().left
       					- (fileList.width() / 2 - focusObj.width() / 2);
       			var top = focusObj.offset().top + focusObj.height() + 6;
       			if (top + fileList.height() > $(window).height()) {
       				top = $(window).height() - (fileList.height() + 6);
       			}
       			if (left + fileList.width() > $(window).width()) {
       				left = $(window).width() - (fileList.width() + 6);
       			}
       			if(left<=0){
       				left=focusObj.offset().left;
       			}
       			
       			fileList.css({
       				left : left,
       				top : top
       			});
       			fileList.css({
       				opacity : 0
       			});
       			fileList.animate({
       				opacity : 1
       			}, 1000);
           	        	 
           	         }
           	     });
					
				}else{
					/*var regex=/[a-zA-z]{32}\-{1}[\W\w]+\.{1}[a-zA-z]+$/;*/
					var fileInfos={};
					if(typeof(fileInfosStr) == "object" && Object.prototype.toString.call(fileInfosStr).toLowerCase() == "[object object]"){
						fileInfos=[fileInfosStr];
					}
					else{
                      fileInfos = eval("(" + fileInfosStr + ")");
                     }
					$(fileInfos)
							.each(
									function(i, fileInfo) {
                                        var fileInfoTypeArr=fileInfo.type.split("/");
                                        var fileInfoType = fileInfo.type;
										var tr = $("<tr></tr>").appendTo(
												contentTable);
										var nameTd = $("<td></td>")
												.appendTo(tr);
										var nameLink = $(
												"<a href='"
														+ docDir
														+ "/"
														+ fileInfo.icoPath
														+ "' download='' target='downLoadProjectDocFileResult' >"
														// + fileInfo.icoPath
														// 		.substr(32 + 1)
														+ "</a>").appendTo(nameTd);

                                        var dl = $('<dl></dl>').appendTo(nameLink);
                                        var dt = $('<dt class="'+fileInfoType+'"></dt>').appendTo(dl);
                                        var fileName = $('<span class="downLoadFileName">'+fileInfo.icoPath.substr(32 + 1)+'</span>').appendTo(dt);
                                        var fileSize = $('<span class="downLoadFileSize">文件大小：'+new Number(fileInfo.size/1024).toFixed(2)+'KB</span>').appendTo(dt);
                                        var fileDownloadBtn = $("<dd><span></span></dd>").appendTo(dl);


									});
					
					var left = focusObj.offset().left
					- (fileList.width() / 2 - focusObj.width() / 2);
			var top = focusObj.offset().top + focusObj.height() + 6;
			if (top + fileList.height() > $(window).height()) {
				top = $(window).height() - (fileList.height() + 6);
			}
			if (left + fileList.width() > $(window).width()) {
				left = $(window).width() - (fileList.width() + 6);
			}
			if(left<=0){
				left=focusObj.offset().left;
			}
			
			fileList.css({
				left : left,
				top : top
			});
			fileList.css({
				opacity : 0
			});
			fileList.animate({
				opacity : 1
			}, 1000);

					
				}
			
			}

		}//filesPanel

	}

})();

$(document).bind('click',function(){
    $(".tempFileList").fadeOut(100);
    $("#slideContent").remove()
});